import nami from '../../nami/index';
import utils from '../../utils/utils';

var app = getApp();
let chooseYear = null;
let chooseMonth = null;
let lastIdx = null;

Page({
  data: {
    userInfo: {},
    hasEmptyGrid: false,
    showPicker: false,
    char_lt: '<',
    char_rt: '>',
    diary: {}
  },

  /**
   * 进入页面
   */
  onLoad() {
    var that = this;
    wx.getUserInfo({
      success: function(res) {
        that.setData({
          userInfo: res.userInfo
        });
      }
    });
    const date = new Date();
    const curYear = date.getFullYear();
    const curMonth = date.getMonth() + 1;
    const weeksCh = [ '日', '一', '二', '三', '四', '五', '六' ];
    that.calculateEmptyGrids(curYear, curMonth);
    that.calculateDays(curYear, curMonth);
    that.setData({
      curYear,
      curMonth,
      weeksCh
    });
  },

  getThisMonthDays(year, month) {
    return new Date(year, month, 0).getDate();
  },
  getFirstDayOfWeek(year, month) {
    return new Date(Date.UTC(year, month - 1, 1)).getDay();
  },
  calculateEmptyGrids(year, month) {
    var that = this;
    const firstDayOfWeek = that.getFirstDayOfWeek(year, month);
    let empytGrids = [];
    if (firstDayOfWeek > 0) {
      for (let i = 0; i < firstDayOfWeek; i++) {
        empytGrids.push(i);
      }
      that.setData({
        hasEmptyGrid: true,
        empytGrids
      });
    } else {
      that.setData({
        hasEmptyGrid: false,
        empytGrids: []
      });
    }
  },
  calculateDays(year, month) {
    let days = [];
    var that = this;

    const thisMonthDays = that.getThisMonthDays(year, month);

    for (let i = 1; i <= thisMonthDays; i++) {
      days.push({
        day: i,
        choosed: false
      });
    }

    that.setData({
      days
    });
  },
  handleCalendar(e) {
    var that = this;
    const handle = e.currentTarget.dataset.handle;
    const curYear = that.data.curYear;
    const curMonth = that.data.curMonth;
    if (handle === 'prev') {
      let newMonth = curMonth - 1;
      let newYear = curYear;
      if (newMonth < 1) {
        newYear = curYear - 1;
        newMonth = 12;
      }

      that.calculateDays(newYear, newMonth);
      that.calculateEmptyGrids(newYear, newMonth);

      that.setData({
        curYear: newYear,
        curMonth: newMonth
      });
    } else {
      let newMonth = curMonth + 1;
      let newYear = curYear;
      if (newMonth > 12) {
        newYear = curYear + 1;
        newMonth = 1;
      }

      that.calculateDays(newYear, newMonth);
      that.calculateEmptyGrids(newYear, newMonth);

      that.setData({
        curYear: newYear,
        curMonth: newMonth
      });
    }
  },

  tapDayItem(e) {
    var that = this;
    const days = that.data.days;
    const idx = e.currentTarget.dataset.idx;
    if (lastIdx != null) {
      if(lastIdx != idx) {
        days[lastIdx].choosed = false;
      }
    }
    lastIdx = idx;
    days[ idx ].choosed = !days[ idx ].choosed;


    that.setData({
      days,
    });
    var choosedDay = new Date(that.data.curYear,that.data.curMonth - 1,days[idx].day).format('yyyy-MM-dd');
    console.log(choosedDay);
    nami.request({
      url: '/request/diary/showDiary.groovy',
      data: {
        DATE: choosedDay
      },
      success: function (res) {
        if (res.data.length == 0) {
          wx.showModal({
            title: '提示',
            content: '当日没有日记哦，请重新选择日期',
            showCancel: false,
            success: function(resp) {
              if (resp.confirm) {
                console.log('用户点击确定')
              }
            }
          });
        } else {
            console.log(res.data[0]);
            var diary = {
              WORK: res.data[0].WORK,
              HEALTH: res.data[0].HEALTH,
              FAMILY: res.data[0].FAMILY,
              STUDY: res.data[0].STUDY,
              HOBBY: res.data[0].HOBBY,
              MONEY: res.data[0].MONEY,
              RELATIONSHIP: res.data[0].RELATIONSHIP,
              PLAN: res.data[0].PLAN,
              ANNIVERSARY: res.data[0].ANNIVERSARY
            }           
            that.setData({
              diary: diary
            });
        }

      }
    });
  },


  chooseYearAndMonth() {
    var that = this;
    const curYear = that.data.curYear;
    const curMonth = that.data.curMonth;
    let pickerYear = [];
    let pickerMonth = [];
    for (let i = 1900; i <= 2100; i++) {
      pickerYear.push(i);
    }
    for (let i = 1; i <= 12; i++) {
      pickerMonth.push(i);
    }
    const idxYear = pickerYear.indexOf(curYear);
    const idxMonth = pickerMonth.indexOf(curMonth);
    that.setData({
      pickerValue: [ idxYear, idxMonth ],
      pickerYear,
      pickerMonth,
      showPicker: true,
    });
  },
  pickerChange(e) {
    var that = this;
    const val = e.detail.value;
    chooseYear = that.data.pickerYear[val[0]];
    chooseMonth = that.data.pickerMonth[val[1]];
  },
  tapPickerBtn(e) {
    var that = this;
    const type = e.currentTarget.dataset.type;
    const o = {
      showPicker: false,
    };
    if (type === 'confirm') {
      o.curYear = chooseYear;
      o.curMonth = chooseMonth;
      that.calculateEmptyGrids(chooseYear, chooseMonth);
      that.calculateDays(chooseYear, chooseMonth);
    }

    that.setData(o);
  },
})
